package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageResult<Department> pageResult = departmentService.query(qo);
        List<Department> departments = departmentService.listAll();
        /*for (Department department : departments) {
            department.getId();
            department.getName();
            department.getSn();
        }*/


        System.out.println(pageResult);
        model.addAttribute("pageResult",pageResult);
        return "department/list";
    }



    @RequestMapping("/delete")
    @ResponseBody
    @RequirePermission(name = "部门删除",expression = "department:delete")
    public JsonResult delete(Long id){
        try {
            departmentService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败",false);
        }

    }




    @RequestMapping("/input")
    @RequirePermission(name = "部门添加",expression = "department:input")
    public String input(Long id,Model model){
        if(id!=null){
            Department department = departmentService.get(id);
            model.addAttribute("department",department );
        }
        return "department/input";
    }
    @RequestMapping("saveOrUpdate")
    @RequirePermission(name = "部门保存或修改",expression = "department:saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId()==null){
            departmentService.save(department);
        }else{
            departmentService.update(department);
        }
        return "redirect:/department/list.do";
    }

}
